﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace OA
{
    public class HttpHelper
    {
        #region 初始化参数
        public const string OALOGIN = "http://oa.hdsxtech.com:8080/logincheck.php";
        public const string OAONLINE = "http://oa.hdsxtech.com:8080/general/ipanel/user/user_count.php";
        public const string OASIGN1 = "http://oa.hdsxtech.com:8080/general/attendance/personal/duty/submit.php?DYTYFLAG=0&REGISTER_TYPE=1";
        public const string OASIGN2 = "http://oa.hdsxtech.com:8080/general/attendance/personal/duty/submit.php?DYTYFLAG=0&REGISTER_TYPE=2";
        public const string OASIGN3 = "http://oa.hdsxtech.com:8080/general/attendance/personal/duty/submit.php?DYTYFLAG=0&REGISTER_TYPE=3";
        public const string OASIGN4 = "http://oa.hdsxtech.com:8080/general/attendance/personal/duty/submit.php?DYTYFLAG=0&REGISTER_TYPE=4";
        public const string OAMYSIGN = "http://oa.hdsxtech.com:8080/general/attendance/personal/duty/";
        public const string OAMYTASK = "http://oa.hdsxtech.com:8080/general/project/task/query.php?startrecord=1&endrecord=10";
        public const string OAMYLEAVE = "http://oa.hdsxtech.com:8080/general/attendance/personal/leave/";
        public static string OACookies = string.Empty;
        #endregion
        /// <summary>
        /// 无参数构造函数
        /// </summary>
        public HttpHelper()
        {

        }
        #region 签到类型枚举
        public enum ESignType
        {
            /// <summary>
            /// 上午上班
            /// </summary>
            Sign1 = 1,
            /// <summary>
            /// 上午下班
            /// </summary>
            Sign2 = 2,
            /// <summary>
            /// 下午上班
            /// </summary>
            Sign3 = 3,
            /// <summary>
            /// 下午下班
            /// </summary>
            Sign4 = 4
        }
        #endregion
        /// <summary>
        /// 打卡请求Get
        /// </summary>
        /// <param name="signtype">枚举4次操作</param>
        /// <returns></returns>
        public static CookiesEntity GetSignOpeate(ESignType signtype, string cookie)
        {
            CookiesEntity entity = new CookiesEntity();
            switch (signtype)
            {
                case ESignType.Sign1:
                    entity.ResultMessage = GetSignOpeateByGet(OASIGN1, cookie);
                    entity.ResultSignMessage = "上午上班";
                    break;
                case ESignType.Sign2:
                    entity.ResultMessage = GetSignOpeateByGet(OASIGN2, cookie);
                    entity.ResultSignMessage = "上午下班";
                    break;
                case ESignType.Sign3:
                    entity.ResultMessage = GetSignOpeateByGet(OASIGN3, cookie);
                    entity.ResultSignMessage = "下午上班";
                    break;
                case ESignType.Sign4:
                    entity.ResultMessage = GetSignOpeateByGet(OASIGN4, cookie);
                    entity.ResultSignMessage = "下午下班";
                    break;
                default:
                    entity.ResultMessage = "不存在的参数";
                    break;
            }
            return entity;
        }
        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static CookiesEntity GetLoginCookie(string username, string password)
        {
            CookiesEntity cookiesentity = new CookiesEntity();
            byte[] paramsbyte = Encoding.Default.GetBytes("UNAME=" + username + "&PASSWORD=" + password + "&KEY_SN=&KEY_USER=&KEY_DIGEST=");
            HttpWebRequest httpwebrequest = WebRequest.Create(OALOGIN) as HttpWebRequest;
            CookieCollection cookiecollection = new CookieCollection();
            httpwebrequest.CookieContainer = new CookieContainer();
            httpwebrequest.CookieContainer.Add(cookiecollection);
            httpwebrequest.Method = "POST";
            httpwebrequest.ContentType = "application/x-www-form-urlencoded";
            httpwebrequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
            httpwebrequest.Accept = "text/html, application/xhtml+xml, */*";
            httpwebrequest.Referer = "http://oa.hdsxtech.com:8080/";
            httpwebrequest.ContentLength = paramsbyte.Length;
            Stream stream = httpwebrequest.GetRequestStream();
            stream.Write(paramsbyte, 0, paramsbyte.Length);
            stream.Close();
            HttpWebResponse httpwebresponse = httpwebrequest.GetResponse() as HttpWebResponse;
            Stream responsestream = httpwebresponse.GetResponseStream();
            StreamReader streamreader = new StreamReader(responsestream, Encoding.Default);
            string resultmsg = streamreader.ReadToEnd();
            responsestream.Close();
            streamreader.Close();
            if (resultmsg.IndexOf("正在进入OA系统，请稍候...") != -1)
            {
                OACookies = httpwebrequest.CookieContainer.GetCookieHeader(httpwebrequest.RequestUri);
                cookiesentity.CookieStatus = true;
                cookiesentity.GetCookie = OACookies;
            }
            else
            {
                cookiesentity.CookieStatus = false;
                cookiesentity.GetCookie = httpwebrequest.CookieContainer.GetCookieHeader(httpwebrequest.RequestUri);
            }
            httpwebresponse.Close();
            httpwebrequest.Abort();
            return cookiesentity;
        }
        /// <summary>
        /// 获取在线人数
        /// </summary>
        /// <returns></returns>
        public static string GetOnlineCount(string cookie)
        {
            HttpWebRequest httpwebrequest = WebRequest.Create(OAONLINE) as HttpWebRequest;
            httpwebrequest.Method = "GET";
            httpwebrequest.Referer = "http://oa.hdsxtech.com:8080/general/";
            httpwebrequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
            httpwebrequest.Headers.Add("Cookie", cookie);
            HttpWebResponse httpwebresponse = httpwebrequest.GetResponse() as HttpWebResponse;
            Stream streamresponse = httpwebresponse.GetResponseStream();
            StreamReader streamreader = new StreamReader(streamresponse, Encoding.Default);
            string resultmsg = streamreader.ReadToEnd();
            streamreader.Close();
            streamresponse.Close();
            httpwebresponse.Close();
            httpwebrequest.Abort();
            return resultmsg;
        }
        /// <summary>
        /// 签到请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        private static string GetSignOpeateByGet(string url, string cookie)
        {
            string resultmsg = string.Empty;
            HttpWebRequest httpwebrequest = WebRequest.Create(url) as HttpWebRequest;
            httpwebrequest.Method = "GET";
            httpwebrequest.Referer = "http://oa.hdsxtech.com:8080/general/";
            httpwebrequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
            httpwebrequest.Headers.Add("Cookie", cookie);
            HttpWebResponse httpwebresponse = httpwebrequest.GetResponse() as HttpWebResponse;
            Stream streamresponse = httpwebresponse.GetResponseStream();
            StreamReader streamreader = new StreamReader(streamresponse, Encoding.Default);
            resultmsg = streamreader.ReadToEnd();
            streamreader.Close();
            streamresponse.Close();
            httpwebresponse.Close();
            httpwebrequest.Abort();
            return resultmsg;
        }
        /// <summary>
        /// 获取我的签到
        /// </summary>
        /// <returns></returns>
        public static string GetMySign(string cookie)
        {
            HttpWebRequest httpwebrequest = WebRequest.Create(OAMYSIGN) as HttpWebRequest;
            httpwebrequest.Method = "GET";
            httpwebrequest.Referer = "http://oa.hdsxtech.com:8080/general/";
            httpwebrequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
            httpwebrequest.Headers.Add("Cookie", cookie);
            HttpWebResponse httpwebresponse = httpwebrequest.GetResponse() as HttpWebResponse;
            Stream streamresponse = httpwebresponse.GetResponseStream();
            StreamReader streamreader = new StreamReader(streamresponse, Encoding.Default);
            string resultmsg = streamreader.ReadToEnd();
            streamreader.Close();
            streamresponse.Close();
            httpwebresponse.Close();
            httpwebrequest.Abort();
            return resultmsg;
        }
        /// <summary>
        /// 获取我的任务
        /// </summary>
        /// <returns></returns>
        public static string GetMyTask(string cookie)
        {
            HttpWebRequest httpwebrequest = WebRequest.Create(OAMYTASK) as HttpWebRequest;
            httpwebrequest.Method = "GET";
            httpwebrequest.Referer = "http://oa.hdsxtech.com:8080/general/project/task/task_doing.php";
            httpwebrequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
            httpwebrequest.Headers.Add("Cookie", cookie);
            HttpWebResponse httpwebresponse = httpwebrequest.GetResponse() as HttpWebResponse;
            Stream streamresponse = httpwebresponse.GetResponseStream();
            StreamReader streamreader = new StreamReader(streamresponse, Encoding.Default);
            string resultmsg = streamreader.ReadToEnd();
            streamreader.Close();
            streamresponse.Close();
            httpwebresponse.Close();
            httpwebrequest.Abort();
            return resultmsg;
        }
        /// <summary>
        /// 获取我的倒休
        /// </summary>
        /// <returns></returns>
        public static string GetMyLeave(string cookie)
        {
            HttpWebRequest httpwebrequest = WebRequest.Create(OAMYLEAVE) as HttpWebRequest;
            httpwebrequest.Method = "GET";
            httpwebrequest.Referer = "http://oa.hdsxtech.com:8080/general/";
            httpwebrequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
            httpwebrequest.Headers.Add("Cookie", cookie);
            HttpWebResponse httpwebresponse = httpwebrequest.GetResponse() as HttpWebResponse;
            Stream streamresponse = httpwebresponse.GetResponseStream();
            StreamReader streamreader = new StreamReader(streamresponse, Encoding.Default);
            string resultmsg = streamreader.ReadToEnd();
            streamreader.Close();
            streamresponse.Close();
            httpwebresponse.Close();
            httpwebrequest.Abort();
            return resultmsg;
        }
    }
}
